package ct.zxd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ct.zxd.db.TUserDao;

public class HandleUser extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UFT-8");
		response.setContentType("application/json;charset=utf-8;");
			
		String flag = request.getParameter("flag");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		if (name != null) {
			name = URLEncoder.encode(name, "utf-8");
		}
		String icon = request.getParameter("icon");
		System.out.println("-------------------- ");
		System.out.println("flag "+flag);
		System.out.println("id "+id);
		System.out.println("name "+name);
		System.out.println("icon "+icon);
		
		PrintWriter pw = response.getWriter();
		TUserDao ts = new TUserDao();
		
		try {
			
			if("add".equals(flag)) {
				pw.write(ts.insertUserMark(id, name, icon) > 0 ?  "ok" : "no"); 
			}
			
			if("find".equals(flag)) {
				pw.write(ts.getUserName(id));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			pw.write("no");
		}
		
		
		
		
		
	}
	

}
